from flask_restful import Resource, reqparse,request
from ....Model.Models import Admin as AdminModel
from ....utils import timestamp,response
from flask_restful_swagger import swagger

''' 
管理员表l
acoount = 账户
password = 密码
isadmin = 权限等级
'''
success = 1
notfond = -1
error = 0

# admin_poot_parser = reqparse.RequestParser()
# admin_poot_parser.add_argument('account', type=str)
# admin_poot_parser.add_argument('password', type=str)
# admin_poot_parser.add_argument('isadmin', type=int)

class Adminlist(Resource):
    def __init__(self, db, auth=None):
        self.db = db
  
    def get(self):#获取列表
        return response(success,None,[each.as_dict() for each in self.db.session.query(AdminModel).all()])
    

    def post(self):#添加
        try:
            args = request.form
            self.db.session.add(
                    AdminModel(
                        account=args['account'],
                        password=args['password'],
                        isadmin =args['isadmin'],
                        update_at=timestamp(),
                        create_at=timestamp()
                    )
                )
            self.db.session.commit
            return response(success,None,None)  #返回状态
        except Exception as er:
            return response(error,er,None)
       


class Admin(Resource):

    def __init__(self, db, auth=None):
        self.db = db
    def get(self,admin_id):#查
        return response(success,None,self.db.session.query(AdminModel).filter_by(id=admin_id).first().as_dict())
    def delete(self,admin_id):#删
        try:
            self.db.session.delete(
                self.db.session.query(AdminModel).filter_by(id=admin_id).first()
            )
            self.db.session.commit()
            return response(success,None,None)  #返回状态
        except Exception as er:
            return response(error,er,None)
       


    def put(self,admin_id):#改
        try:
            admin_obj = self.db.session.query(AdminModel).filter_by(id=admin_id).first()
            args = request.form
            if(args['account'] != None):
                admin_obj.acoount = args['acoount']
            if(args['password'] != None):
                admin_obj.hash_password(args['password'])
            if(args['isadmin'] != None):
                admin_obj.isadmin = args['isadmin']
            admin_obj.update_at = timestamp()
            self.db.session.commit()
            return response(success,None,None)  #返回状态
        except Exception as er:
            return response(error,er,None)
       